using System;
using System.Web;

namespace YN.Utils.Web
{
    public class CookieHelper
    {
        public static void SaveCookie(string cookieName, string[] paramNames, string[] paramValues, DateTime expires)
        {
            HttpCookie cookie = new HttpCookie(cookieName);

            for (int index = 0; index < paramNames.Length; index++)
            {
                cookie.Values[paramNames[index]] = paramValues[index];
            }

            if (HttpContext.Current.Request.Cookies[cookieName] != null)
                HttpContext.Current.Response.Cookies.Remove(cookieName);

            cookie.Expires = expires;

            HttpContext.Current.Response.Cookies.Add(cookie);

            HttpContext.Current.Response.Flush();
        }

        public static void SaveCookie(string cookieName, string paramName, string paramValue, DateTime expires)
        {
            SaveCookie(cookieName, new string[] { paramName }, new string[] { paramValue }, expires);
        }

        public static void ClearCookie(string cookieName)
        {
            HttpCookie cookie = new HttpCookie(cookieName);

            cookie.Expires = DateTime.Now.AddYears(-3);

            HttpContext.Current.Response.Cookies.Add(cookie);

            HttpContext.Current.Response.Flush();
        }
    }
}
